package cn.duli.xianer.controller;

import cn.duli.xianer.service.FileService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/upload")
public class TestController {

    @Autowired
    FileService fileService;

    @Value("${baseUploadUrl}")
    private String url;

    @Value("${qiniu.path}")
    private String path;

    /**
     * 多图上传到七牛云
     * @param upfiles
     * @return
     * @throws IOException
     */
//    @PostMapping(value = "/uploadImg")
//    public List<Object> uploadImg(@RequestParam(value = "files")MultipartFile upfiles[]) throws IOException {
//        List<Object> list = new ArrayList<>();
//        try{
//            if(upfiles!=null) {
//                for (int i=0;i<upfiles.length;i++) {
//                    if (!upfiles[i].isEmpty()) {
//                        String fileName = upfiles[i].getOriginalFilename();
//                        File file = new File(url + fileName);
//                       // 将MulitpartFile文件转化为file文件格式
//                        upfiles[i].transferTo(file);
//                        Map response = fileService.uploadFile(file);
//                        Object imageName = response.get("imgName");
//                        System.out.println(path + "/" + imageName);
//                        // 图片地址
//                        String picture_url = path + "/" + imageName;
//                        list.add(picture_url);
//                    }
//                }
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return list;
//    }


}
